<!-- 饲料管理 -->
<template>
  <!-- <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns search-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName === '1'">收起筛选</p>
              <p v-if="activeName === ''">展开筛选</p>
            </div>
          </template>
<div class="search-btn search-btns" @click.stop="queryPageList(), searchPage()">
  <i class="el-icon-circle-check"></i>
  <p>查询</p>
</div>
<div class="select-box-content">
  <div class="select-box-content-name">饲料名称</div>
  <el-select filterable clearable v-model="searchParams.materialId" placeholder="请选择">
    <el-option v-for="(item, index) of materialList" :key="index + 'area'" :label="item.name"
      :value="item.materialId" />
  </el-select>
</div>
<div class="select-box-content">
  <div class="select-box-content-name">品种名称</div>
  <el-select filterable clearable v-model="searchParams.varietyId" placeholder="请选择">
    <el-option v-for="(item, index) of varietyList" :key="index + 'area'" :label="item.varietyName"
      :value="item.fishVarietyId" />
  </el-select>
</div>
</el-collapse-item>
</el-collapse>
</template>
<template v-slot:btnsBar>
      <ActionBar :add-active="addActive" :del-active="delActive" :page-no="161" @add="add" @del="del" />
    </template>
<template v-slot:content>
      <Table :propList="propList" :listData="tableData" :showSelectColumn="true"
        @handleSelectionChange="handleSelectionChange">
        <template v-slot:operation="{ row }">
          <div class="table-btns-box" v-show="row.feedWorkMaterialConfigId">
            <div class="table-btns edit-btns" @click="edit(row)" v-if="judgePermission(161, 'edit')">
              <p>修改</p>
            </div>
          </div>
        </template>
<template v-slot:footer>
          <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
            @changeCurrentPage="changeCurrentPage" />
        </template>
</Table>
<AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText" :edit-object="editObject"
  :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
</template>
</BaseMainSlot> -->

  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search"
        @isUnfold="(v) => { unfoldFlag = v }">
      </SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <Table v-loading="loading" :propList="propList" :listData="tableData" :showSelectColumn="true" :showFooter="false"
        @handleSelectionChange="handleSelectionChange">
        <template v-slot:operation="{ row }">
          <el-button type="primary" plain @click="edit(row)">修改</el-button>
          <el-button type="danger" plain @click="delOne(row)">删除</el-button>
        </template>
        <template v-slot:footer>
          <Pagination :page-size="pageSize" :current-page="currentPage" :total-size="totalSize"
            @changeCurrentPage="changeCurrentPage" />
        </template>
      </Table>
    </template>
    <template v-slot:pagination>
      <Pagination @current-change="changeCurrentPage" :page="currentPage" :size="pageSize" :total="totalSize">
      </Pagination>
    </template>
    <template v-slot:dialogModule>
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </MainSlot>
</template>

<script>

import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import SearchForm from '@/components-production-management/SearchForm';
import BtnSlot from '@/components-production-management/BtnSlot';

import Table from "@/components/table/index.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

import FodderApi from "@/api/basic-info-manage/fodder-management";
import MaterialApi from "@/api/basic-info-manage/material";
import FishVarietyApi from "@/api/basic-info-manage/fishVariety";
export default {
  name: "AreaManage",
  components: { BtnSlot, Pagination, SearchForm, MainSlot, Table, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      unfoldFlag: false,
      loading: false,
      btnList: ['add', 'delete'],
      // 控制弹出框显示隐藏
      showAddEditDialog: false,
      // 查询条件：
      searchParams: {},
      materialList: [],
      varietyList: [],
      propList: [
        { prop: "materialName", label: "饲料名称", minWidth: "80" },
        { prop: "varietyName", label: "关联品种", minWidth: "80" },
        { prop: "feedRatio", label: "饲料转化比", minWidth: "80" },
        { label: "操作", minWidth: "80", slotName: "operation" },
      ],
      checkedIdList: [],
      formItemList: [
        {
          label: '饲料名称',
          type: 'select',
          selectOptions: [],
          param: 'materialId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        },
        {
          label: '品种名称',
          type: 'select',
          selectOptions: [],
          param: 'varietyId',
          defaultSelect: '', // 下拉框默认选中项
          multiple: false,
          isShow: true
        }
      ],
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },
  watch: {},
  created() {
    this.queryPageList();
    this.queryMaterial()
    this.queryVarietyApi()
  },
  mounted() { },
  methods: {
    setFormOptions() {
      let list = JSON.parse(JSON.stringify(this.formItemList));
      this.formItemList = []
      list.forEach((item, index) => {
        if (item.param.indexOf('materialId') !== -1) {
          let selectOptions = this.materialList.map(data => {
            return {
              text: data.name, value: data.materialId
            }
          })
          item.defaultSelect = this.searchParams[item.param];
          item.selectOptions = selectOptions;
        };
        if (item.param.indexOf('varietyId') !== -1) {
          let selectOptions = this.varietyList.map(data => {
            return {
              text: data.varietyName, value: data.fishVarietyId
            }
          })
          item.defaultSelect = this.searchParams[item.param];
          item.selectOptions = selectOptions;
        };

      });
      this.$nextTick(() => {
        this.$set(this, 'formItemList', list)
        console.log(this.formItemList)
      })
    },
    //获取选中Btn
    getBtnEvent(flag) {
      if (flag == 'add') {
        this.add()
      } else if (flag == 'delete') {
        this.delmessage()
      }
    },
    search(data) {
      this.currentPage = 1;
      this.searchParams = { ...this.searchParams, ...data }
      this.queryPageList();
    },
    queryMaterial() {
      MaterialApi.queryByCondition({ materialTypeNo: "feed" }).then(res => {
        this.materialList = res.data;
        this.setFormOptions()
      })
    },
    queryVarietyApi() {
      FishVarietyApi.queryByCondition({ materialTypeNo: "feed" }).then(res => {
        this.varietyList = res.data
        this.setFormOptions()
      })
    },
    queryPageList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: { ...this.searchParams }
      };
      this.loading = true;
      FodderApi.queryPageByCondition(params).then(res => {
        this.loading = false;
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
        }
      }).catch(err => {
        this.loading = false;
      });
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    handleEdit(row) {
      if (row.feedWorkMaterialConfigId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.addActive = true;
        this.editActive = true;
      } else {
        this.editObject = {};
        this.addActive = true;
        this.editActive = false;
      }
    },
    selectAll(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.feedWorkMaterialConfigId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach(item => {
        if (!item.feedWorkMaterialConfigId) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map(item => {
        if (item.feedWorkMaterialConfigId) {
          return item.feedWorkMaterialConfigId;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",

      })
        .then(() => {
          FodderApi.delById({ id: data.feedWorkMaterialConfigId }).then(res => {
            if (res.success === true) {
              this.$message.success("删除成功!");
              this.queryPageList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 表格删除
    delmessage() {
      if (!this.checkedIdList.length) return;
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          FodderApi.delListById({ idList: this.checkedIdList }).then(res => {
            if (res.success) {
              this.$message.success("删除成功");
              this.queryPageList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });

    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.feedWorkMaterialConfigId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
